# -*- coding: UTF-8 -*-
# 案例分析
from numpy import array
from numpy.random import normal
from matplotlib import pyplot
import numpy as np
import pandas as pd

def getData():
    heights=[]
    weights=[]
    books=[]
    N=10000
    for i in range(N):
        while True:
            #身高服从均值为172，标准差为6的正态分布
            height=normal(172,6)
            if 0<height:
                break
        while True:
            #体重由身高作为自变量的线性回归模型产生，误差服从标准正态分布
            weight=(height-80)*0.7+normal(0,1)
            if 0<weight:
                break
        while True:
            #借阅量服从均值为20，标准差为5的正态分布
            number=normal(20,5)
            if 0<=number and number<=50:
                book='E' if number<10 else ('D' if number<15 else ('C' if number<20 else ('B' if number<25 else 'A')))
                break
        heights.append(height)
        weights.append(weight)
        books.append(book)
    return array(heights),array(weights),array(books)
heights,weights,books=getData()

#绘制直方图
def drawHist(heights):
    #创建直方图
    #第一个参数为待绘制的定量数据，不同于定性数据，这里并没有实现进行频数统计
    #第二个参数为划分的区间个数
    pyplot.hist(heights,100)
    pyplot.xlabel('Heights')
    pyplot.ylabel('Frequency')
    pyplot.title('Height of Students')
    pyplot.show()


# drawHist(heights)
drawHist(weights)
# drawHist(books)

#绘制散点图
def drawScatter(heights,weights):
    #创建散点图
    #第一个参数为点的横坐标
    #第二个参数为点的纵坐标
    pyplot.scatter(heights,weights)
    pyplot.xlabel('Heights')
    pyplot.ylabel('Weight')
    pyplot.title('Heights & Weight of Students')
    pyplot.show()
# drawScatter(heights,weights)